package com.oyb.dao;

import com.oyb.entity.Course;
import com.oyb.entity.Page;
import com.oyb.util.ConnectionUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CourseDao {
    //查询课程的总数
    public int querypagesum() throws SQLException {
        Connection connection = ConnectionUtils.getconnection();
        String sql = "select count(*) from course";
        PreparedStatement statement = connection.prepareStatement(sql);
        ResultSet resultSet = statement.executeQuery();
        if(resultSet.next()){
            return resultSet.getInt(1);
        }
        ConnectionUtils.close(statement,connection,resultSet);
        return 0;
    }
    //分页查询课程——课程数据
    public List<Course> querypagedatas(Page p) throws SQLException {
        List<Course> courseList = new ArrayList<>();
        Connection connection = ConnectionUtils.getconnection();
        String sql = "select * from course limit ?,?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setObject(1,(p.getPage()-1)*p.getPageSize());
        statement.setObject(2,p.getPageSize());
        ResultSet resultSet = statement.executeQuery();
        while (resultSet.next()){
            Course course = new Course();
            course.setId( resultSet.getInt("id"));
            course.setCoursename(resultSet.getString("coursename"));
            courseList.add(course);
        }
        ConnectionUtils.close(statement,connection,resultSet);
        return courseList;
    }
}
